<?php
namespace Communal\Controller;
use Common\Controller\CommonController;

/**
 * 安装模块
 */
class InstallController extends CommonController {
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
	}
	
	public function installAction(){
		header('Content-Type:text/html; charset=utf-8');
		$this->_installDb();
	}
	
	public function flushcacheAction(){
		header('Content-Type:text/html; charset=utf-8');
		$this->_flushCacheApp();
	}
	
	//清空缓存
	protected function _flushCacheApp(){
		$this->_mycache->FLUSHALL();
		exit('缓存清空成功');
	}
	
	//安装数据库
	protected function _installDb(){
		//安装完成后，请及时删除./sql/qyx_base.sql
		$lines=file("./sql/qyx_base.sql");
		$sqlstr="";
		foreach($lines as $line){
			$line=trim($line);
			if($line!=""){
				$sqlstr.=$line;
			}
		}
		
		$sqlstr=rtrim($sqlstr,";");
		
		$sqls=explode(";",$sqlstr);
		
		//默认密码:123456
		$pwd = '$2y$10$dvO0WcOthP2QimmsZBcQyuqEV3wLiedqCBpcFxBgfAfvfYj4DxkaC';
		$sqls[] = "insert into qyx_cust(custid,nickname,realname,pinyinname,custtype,status,sex,mobile,email,password,issuper,idenum) values(10000,'许愿池团队','许愿池团队','qiyunxintuanddui',3,1,0,'13600000000','admin@qiyunxin.com','{$pwd}',1,10000)";
		$sqls[] = "insert into qyx_cust_ext(custid) values(10000)";
		$sqls[] = "insert into qyx_publicno(publicnoid,custid) values(10000,10000)";
		
		//echo "<pre>";
		//print_r($sqls);
		if(empty($sqls)){
			exit('空文件');
		}
		
		$dsn = C('DB_BASE_DSN');
		
		$_mysqli = new \mysqli($dsn['DB_HOST'],$dsn['DB_USER'],$dsn['DB_PWD']);
		if (mysqli_connect_errno()) {
			exit('连接数据库出错');
		}
		//执行sql语句
		foreach ($sqls as $sql) {
			if(empty($sql)) continue;
			$_mysqli->query($sql.';');
		}
		$_mysqli->close();
		$_mysqli = null;
		
		
		@unlink("./sql/qyx_base.sql");
		exit('安装完成');
	}
}
